package com.stu.day04;

/**
 * @author zy
 * @date 2025/1/27 14:29
 */
public class ArraySearchDemo {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        int target = 8;
        System.out.println(binarySearch(arr, target));
    }

    public static int binarySearch(int[] arr, int target) {
        int low = 0;
        int high = arr.length - 1;
        while (low <= high) {
            int mid = (low + high) >>1;
            if (arr[mid] == target)
                return mid;
            else if (arr[mid] < target)
                low = mid + 1;
            else if (arr[mid] > target) {
                high = mid - 1;
            }
        }
        return -1;
    }
}
